package com.common.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * User     : bing.zhang@suzsoft.com
 * Date     : 2005-10-12 10:42:37
 * Version  : 0.1
 * updateBy :
 * updateDt :
 */
public class FileUtil {

    private FileUtil() {
    }

    private static Log logger = LogFactory.getLog(FileUtil.class);

    /**
     * Write a serializable object into a local file
     * @param obj
     * @param destFileFullPathName
     * @return boolean
     * author: zhangbing
     */
    public static boolean writeSerializableObject2File(Object obj, String destFileFullPathName) {
        ObjectOutputStream out = null;
        try {
            File f = createFile(destFileFullPathName);
            out = new ObjectOutputStream(new FileOutputStream( f ));
            out.writeObject( obj );
            return true;
        } catch (Exception ex) {
            logger.error(ex);
            return false;
        } finally{
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                logger.error(e);
            }
        }
    }

    /**
     * read file to the serialized object
     * @param srcFileFullPathName
     * @return Object
     * author: zhangbing
     */
    public static Object readSerializableObjectFromFile(String srcFileFullPathName) {
        ObjectInputStream in = null;
        try {
            in = new ObjectInputStream(new FileInputStream( srcFileFullPathName ));
            return in.readObject();
        } catch (Exception ex) {
            logger.error(ex);
            return null;
        } finally{
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                logger.error(e);
            }
        }
    }

    /**
     *
     * @param srcFileFullPathName
     * @param zipedFileFullPathName
     * @return boolean
     * author: zhangbing
     */
    public static boolean zipFile(String srcFileFullPathName, String zipedFileFullPathName) {
        ZipOutputStream zipOut = null;
        FileInputStream fin = null;

        try {
            zipOut = new ZipOutputStream(new FileOutputStream( zipedFileFullPathName ));
            fin = new FileInputStream( srcFileFullPathName );

            String fileName = srcFileFullPathName.substring(srcFileFullPathName.lastIndexOf("\\")+1, srcFileFullPathName.length());
            zipOut.putNextEntry( new ZipEntry(fileName) );

            int byteRead;
            byte[] buff = new byte[1024];
            while ((byteRead = fin.read(buff)) != -1) {
                zipOut.write(buff, 0, byteRead);
            }

            return true;
        } catch (Exception ex) {
            logger.error(ex);
            return false;
        } finally{
            try {
                if (fin != null) {
                    fin.close();
                }
                if (zipOut != null) {
                    zipOut.close();
                }
            } catch (IOException e) {
                logger.error(e);
            }
        }
    }

    /**
     *
     * @param zipedFileFullPathName
     * @param upzipFileFullPathName
     * @return boolean
     * author: zhangbing
     */
    public static boolean upzipFile(String zipedFileFullPathName, String upzipFileFullPathName) {
        ZipInputStream zipIn = null;
        FileOutputStream fout = null;

        try {
            zipIn = new ZipInputStream( new FileInputStream(zipedFileFullPathName) );
            File f = new File( upzipFileFullPathName );
                 f.createNewFile();
            fout = new FileOutputStream(f);

            ZipEntry z = zipIn.getNextEntry();
            if (z != null) {
                int byteRead;
                byte[] buff = new byte[1024];
                while ((byteRead = zipIn.read(buff)) != -1) {
                    fout.write(buff, 0, byteRead);
                }
            }

            return true;
        } catch (Exception ex) {
            logger.error(ex);
            return false;
        } finally{
            try {
                if (fout != null) {
                    fout.close();
                }
                if (zipIn != null) {
                    zipIn.close();
                }
            } catch (IOException e) {
                logger.error(e);
            }
        }
    }

    public static void writeStream2File(InputStream inStream, String fileFullPathname) {
        FileOutputStream foutStream = null;
        try {
            File file = new File(fileFullPathname);
            if (! file.exists()) {
                file.createNewFile();
            }

            foutStream = new FileOutputStream(file);
            int byteRead;
            byte[] buff = new byte[1024];
            while ((byteRead = inStream.read(buff)) != -1) {
                foutStream.write(buff, 0, byteRead);
            }
            foutStream.close();
        } catch(Exception ex) {
            logger.error(ex);
        } finally {
            try {
                if (inStream != null) {
                    inStream.close();
                }
                if (foutStream != null) {
                    foutStream.close();
                }
            } catch (IOException e1) {
                logger.debug(e1);
            }
        }
    }

    public static void writeFile2Stream(String fileFullPathName, OutputStream outStream) {
        FileInputStream finStream = null;
        try {
            finStream = new FileInputStream(fileFullPathName);
            int byteRead;
            byte[] buff = new byte[1024];
            while ((byteRead = finStream.read(buff)) != -1) {
                outStream.write(buff, 0, byteRead);
            }
        } catch (Exception e) {
            logger.error(e);
        } finally {
            try {
                if (finStream != null) {
                    finStream.close();
                }
                if (outStream != null) {
                    outStream.close();
                }
            } catch (IOException e1) {
                logger.debug(e1);
            }
        }
    }

    public static void writeStream2Stream(InputStream inStream, OutputStream outStream) {
        try {
            int byteRead;
            byte[] buff = new byte[1024];
            while ((byteRead = inStream.read(buff)) != -1) {
                outStream.write(buff, 0, byteRead);
            }
        } catch (Exception e) {
            logger.error(e);
        } finally {
            try {
                if (inStream != null) {
                    inStream.close();
                }
                if (outStream != null) {
                    outStream.close();
                }
            } catch (IOException e1) {
                logger.debug(e1);
            }
        }
    }

    public static void deleteFile(String fileFullPathName) {
        File f = new File( fileFullPathName );
        if (f.exists()) {
            f.delete();
        }
    }

    public static void deleteFiles(String folderFullPathName) {
        File f = new File( folderFullPathName );
        if (f.isDirectory()) {
            File [] fs = f.listFiles();
            for (int i=0; i<fs.length; i++) {
                fs[i].delete();
            }
        } 
    }

    public static File createFile(String fileFullPathname) {
        File folder = new File(fileFullPathname.substring(0, fileFullPathname.lastIndexOf("\\")));
        File file = new File(fileFullPathname);
        try {
            if(! folder.exists()) {
                folder.mkdirs();
            }

            if (! file.exists()) {
                file.createNewFile();
            }
        } catch(Exception ex) {
            logger.error(ex);
        }
        return file;
    }

    public static boolean existsFile(String fileFullPathname) {
        File file = new File(fileFullPathname);
        return file.exists();
    }
    
}
